package com.woniuxy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dao.MerchantCommissionMapper;
import com.woniuxy.dao.MerchantUsersMapper;
import com.woniuxy.dao.OverOrderMapper;
import com.woniuxy.dao.PreOrderMapper;
import com.woniuxy.dto.DataGridView;
import com.woniuxy.feign.Feigntest;
import com.woniuxy.pojo.MerchantCommission;
import com.woniuxy.pojo.MerchantUsers;
import com.woniuxy.pojo.OverOrder;
import com.woniuxy.pojo.PreOrder;
import com.woniuxy.service.CommissionjobService;
import com.woniuxy.service.SeateService;
import com.woniuxy.service.UserService;
import com.woniuxy.vo.CommissionVo;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Service
public class CommissionjobImpl  implements CommissionjobService {
    @Autowired
    private MerchantCommissionMapper merchantCommissionMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private PreOrderMapper preOrderMapper;
    @Autowired
    private OverOrderMapper overOrderMapper;
    @Autowired
    private MerchantUsersMapper merchantUsersMapper;
    @Autowired
    private Feigntest feigntest;
    @Autowired
    private SeateService seateService;
    @Override
    public DataGridView findCommissionByuid(CommissionVo commissionVo) {
       MerchantUsers merchantUsers =userService.findPuserByid(commissionVo.getMuid());
       commissionVo.setMuid(merchantUsers.getId());
       PageHelper pageHelper=new PageHelper();
       Page<OverOrder> page= PageHelper.startPage(commissionVo.getPage(),commissionVo.getLimit());
       List<MerchantCommission> commissions =merchantCommissionMapper.findCommissionByuid(commissionVo);
        for (MerchantCommission commission : commissions) {
            
        }
       return new DataGridView(page.getTotal(),commissions);
    }

    @Override
    public List<PreOrder> findAllpreorder() {
        return preOrderMapper.findAllpreorder();
    }

    @Override
    public List<OverOrder> findAlloverOrder() {
        return overOrderMapper.findAlloverOrder();
    }

    @Override
    public void updata(MerchantCommission merchantCommission) {
        Integer z =overOrderMapper.findOverOrder(new Integer(merchantCommission.getOrderid()));
        if (z>0){
            merchantCommissionMapper.updateByPrimary(merchantCommission);
        }else{
            merchantCommissionMapper.insert(merchantCommission);
        }
    }

    //提现
    // 转账金额 amount
    //真实姓名 payee_real_name
    // 用户支付宝ID  payee_account
    //订单编号 out_biz_no；
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void commissiontomerchantUser(String amount,Integer id) {

        MerchantUsers merchantUsers=userService.findPuserByid(id);
        Long amount1=new Long(amount);
        String payee_real_name=merchantUsers.getmName();
        //支付账户id固定
        payee_real_name="沙箱环境";
        String payee_account=merchantUsers.getPayeeAccount();
        String out_biz_no = UUID.randomUUID().toString();
        //支付微服务转账
        System.out.println("------");
        feigntest.zhuan(out_biz_no,payee_account,payee_real_name,amount);
        merchantUsers.setCashAdvance(amount1);
        seateService.updatebalence(merchantUsers);
    }


}
